package com.gxuwz.weather.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gxuwz.weather.R;
import com.gxuwz.weather.model.CityWeatherDisplayItem;

import java.util.ArrayList;
import java.util.List;

public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.CityViewHolder> {

    private List<CityWeatherDisplayItem> cityWeatherList;
    private OnItemClickListener listener;
    private OnItemDeleteListener deleteListener;
    private OnSetHomeListener setHomeListener;

    public interface OnItemClickListener {
        void onItemClick(CityWeatherDisplayItem item);
    }

    public interface OnItemDeleteListener {
        void onItemDelete(CityWeatherDisplayItem item, int position);
    }

    public interface OnSetHomeListener {
        void onSetHome(String cityName);
    }

    public CityListAdapter(List<CityWeatherDisplayItem> cityWeatherList, OnItemClickListener listener, OnItemDeleteListener deleteListener) {
        this.cityWeatherList = cityWeatherList;
        this.listener = listener;
        this.deleteListener = deleteListener;
    }

    public void setCityList(List<CityWeatherDisplayItem> newCityWeatherList) {
        if (newCityWeatherList == null) {
            this.cityWeatherList = new ArrayList<>();
        } else {
            this.cityWeatherList = new ArrayList<>(newCityWeatherList);
        }
        notifyDataSetChanged();
    }

    public void setOnSetHomeListener(OnSetHomeListener listener) {
        this.setHomeListener = listener;
    }

    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city_manage, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {
        CityWeatherDisplayItem item = cityWeatherList.get(position);

        // 绑定数据到视图
        holder.tvCityName.setText(item.getCityName());
        holder.tvCurrentTemperature.setText(item.getCurrentTemperature());
        holder.tvWeatherConditionOrAlert.setText(item.getWeatherCondition());
        holder.tvHighLowTemperature.setText(String.format("%s %s", item.getHighTemperature(), item.getLowTemperature()));
        holder.tvDisplayTime.setText(item.getDisplayTime());

        // 根据天气设置背景图和字体颜色
        String weatherCondition = item.getWeatherCondition();
        if (weatherCondition != null) {
            if (weatherCondition.contains("晴")) {
                holder.llWeatherCardContent.setBackgroundResource(R.drawable.bg_sum_72);
            } else if (weatherCondition.contains("云")) {
                holder.llWeatherCardContent.setBackgroundResource(R.drawable.bg_duoyun_72);
            } else if (weatherCondition.contains("雨")) {
                holder.llWeatherCardContent.setBackgroundResource(R.drawable.bg_rain_72);
            } else if (weatherCondition.contains("阴")) {
                holder.llWeatherCardContent.setBackgroundResource(R.drawable.bg_yintian_72);
            } else if (weatherCondition.contains("雾")) {
                holder.llWeatherCardContent.setBackgroundResource(R.drawable.bg_yintian_72);
            }
            else {
                // 如果是其他情况，或者无法匹配到特定背景图，使用默认颜色
                holder.llWeatherCardContent.setBackgroundColor(Color.parseColor("#576170"));
            }
        } else {
            // 默认背景色
            holder.llWeatherCardContent.setBackgroundColor(Color.parseColor("#576170"));
        }

        // 处理预警信息显示
        if (item.getAlertMessage() != null && !item.getAlertMessage().isEmpty()) {
            holder.ivAlertIcon.setVisibility(View.VISIBLE);
            holder.tvWeatherConditionOrAlert.setText(item.getAlertMessage());
        } else {
            holder.ivAlertIcon.setVisibility(View.GONE);
            holder.tvWeatherConditionOrAlert.setText(item.getWeatherCondition());
        }

        // 设置字体颜色
        holder.tvCityName.setTextColor(Color.WHITE);
        holder.tvCurrentTemperature.setTextColor(Color.WHITE);
        holder.tvHighLowTemperature.setTextColor(Color.WHITE);

        int dimWhiteColor = Color.parseColor("#BBFFFFFF");
        holder.tvDisplayTime.setTextColor(dimWhiteColor);
        holder.tvWeatherConditionOrAlert.setTextColor(dimWhiteColor);

        // 设置常驻地星星状态
        String homeCity = android.preference.PreferenceManager.getDefaultSharedPreferences(holder.itemView.getContext()).getString("home_city", "");
        if (item.getCityName().equals(homeCity)) {
            holder.ivSetHome.setImageResource(R.drawable.icon_star_shi); // 实心
        } else {
            holder.ivSetHome.setImageResource(R.drawable.icon_star); // 空心
        }
        holder.ivSetHome.setOnClickListener(v -> {
            if (setHomeListener != null) {
                setHomeListener.onSetHome(item.getCityName());
            }
        });

        // 设置点击监听器
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cityWeatherList.size();
    }

    public class CityViewHolder extends RecyclerView.ViewHolder {
        TextView tvCityName;
        TextView tvCurrentTemperature;
        TextView tvWeatherConditionOrAlert;
        TextView tvHighLowTemperature;
        TextView tvDisplayTime;
        ImageView ivAlertIcon;
        ImageView ivSetHome;
        public LinearLayout llWeatherCardContent;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCityName = itemView.findViewById(R.id.tv_city_name);
            tvCurrentTemperature = itemView.findViewById(R.id.tv_current_temperature);
            tvWeatherConditionOrAlert = itemView.findViewById(R.id.tv_weather_condition_or_alert);
            tvHighLowTemperature = itemView.findViewById(R.id.tv_high_low_temperature);
            tvDisplayTime = itemView.findViewById(R.id.tv_display_time);
            ivAlertIcon = itemView.findViewById(R.id.iv_alert_icon);
            ivSetHome = itemView.findViewById(R.id.iv_set_home);
            llWeatherCardContent = itemView.findViewById(R.id.ll_weather_card_content);
        }
    }
}